<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input
        v-model="params.name"
        style="width: 200px"
        placeholder="请输入姓名"
      ></el-input>
      <el-input
        v-model="params.phone"
        style="width: 200px; margin-left: 5px"
        placeholder="请输入电话"
      ></el-input>
      <el-button type="success" style="margin-left: 10px" @click="findBySearch"
        >查询</el-button
      >
      <el-button type="warning" style="margin-left: 10px" @click="reset"
        >清空</el-button
      >

      <el-button type="primary" style="margin-left: 10px" @click="add"
        >新增</el-button
      >
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <!-- <el-table-column  prop="date"  label="日期" >  </el-table-column> -->
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="sex" label="性别"> </el-table-column>
        <el-table-column prop="age" label="年龄"> </el-table-column>
        <el-table-column prop="phone" label="电话"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger" style="margin-left: 5px;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <div>
      <el-dialog
        title="请填写信息"
        :visible.sync="dialogFormVisible"
        width="30%"
      >
        <el-form :model="form">
          <el-form-item label="姓名" label-width="15%">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" label-width="15%">
            <el-radio-group v-model="form.sex">
              <el-radio :label="3">男</el-radio>
              <el-radio :label="6">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="年龄" label-width="15%">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" label-width="15%">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      params: {
        name: "",
        phone: "",
        pageNum: 1,
        pageSize: 5,
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
    };
  },
  // 页面请求,页面在创建时做的事情
  created() {
    this.findBySearch();
  },
  // 定义页面事件调用方法
  methods: {
    submit() {
      request.post("/admin", this.form).then((res) => {
        if (res.code === "0") {
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.findBySearch();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    add() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    del(id) {
      request.delete("/admin/" + id).then((res) => {
        if (res.code === "0") {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.findBySearch();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    edit(obj) {
      this.form = obj;
      this.dialogFormVisible = true;
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 5,
        name: "",
        phone: "",
      };
      this.findBySearch();
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findBySearch();
    },

    findBySearch() {
      // params: this.params 是用于将查询参数对象传递给 GET 请求的一种方便的方式，以便于构建 URL 查询字符串。
      request
        .get("/admin/search", {
          params: this.params,
        })
        // res在request.js里定义的，res.什么就获取什么数据
        .then((res) => {
          if (res.code === "0") {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else {
          }
        });
    },
  },
};
</script>
